<?php

/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace app\adminapi\controller\v1\platform\wechat;


use app\adminapi\controller\v1\platform\PlatformController;
use app\common\controller\AdminController;
use app\common\model\platform\PlatformSetting;
use hiduoke\services\FormBuilder as Form;
use think\App;

class WechatSetting extends PlatformController
{
    /**
     * @var PlatformSetting
     */
    private $model;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PlatformSetting();
    }

    /**
     * 支付配置表单
     * @return \think\response\Json
     */
    public function pay()
    {
        $plaid = $this->request->param('plaid', 0);
        if ($this->request->isPost()) {
            $formData = $this->request->param('formData', []);
            $res = $this->model->setValueByKey($formData, "pay", $plaid);
            return $this->success("修改成功", $res);
        } else {
            $rule = array();
            //创建表单
            $res = $this->model->getValueByKey("pay", $plaid);
            $form = Form::createForm("/adminapi/platform/wechat/setting/pay?plaid=" . $plaid)->setMethod("post");
            //新增表单
            $rule[] = Form::dkInput('mch_id', '商户号mch_id', $res['mch_id'] ?? '', ['微信支付分配的商户号'])->required();
            $rule[] = Form::dkInput('key', '秘钥key', $res['key'] ?? '', ['设置微信支付的Api秘钥'])->required();
            $rule[] = Form::dkInput('body', '默认描述', $res['body'] ?? '', ['设置支付未指定订单描述时候默认的描述，如果不填写，则默认为：默认商品']);
            $rule[] = Form::dkUpload('pem_cert', 'cert证书', $res['pem_cert'] ?? [])->help(['这是帮助内容'])->fileType('cert')
                ->data(['path' => "wechat/" . $plaid, 'disk' => 'cert'])->required();
            $rule[] = Form::dkUpload('pem_key', "key证书", $res['pem_key'] ?? [])->help(['这是帮助内容'])->fileType('cert')
                ->data(['path' => "wechat/" . $plaid, 'disk' => 'cert'])->required();
            $rule[] = Form::dkInputNumber('settlement_cycle_time', '自定义分账时间', $res['settlement_cycle_time'] ?? '10')
                ->min(10)
                ->effect(['help' => '设置自定义支付后，自定义分账到账的时间,默认T+1为10秒后进行分账处理，单位为：秒']);
            $form->setTitle("支付配置")->setRule($rule);
            return $this->success("", Form::getFormData($form));
        }
    }

    public function transfer()
    {
        $plaid = $this->request->param('plaid', 0);
        if ($this->request->isPost()) {
            $formData = $this->request->param('formData', []);
            $res = $this->model->setValueByKey($formData, "transfer", $plaid);
            return $this->success("修改成功", $res);
        } else {
            $rule = array();
            //创建表单
            $res = $this->model->getValueByKey("transfer", $plaid);
            $form = Form::createForm("/adminapi/platform/wechat/setting/transfer?plaid=" . $plaid)->setMethod("post");
            //新增表单
            $rule[] = Form::radio('state', '企业付款账号', $res['state'] ?? 0)
                ->options([['value' => 0, 'label' => '继承微信支付使用'], ['value' => 1, 'label' => '使用另外的企业付款到零钱']])
                ->appendControl(
                    1,
                    [
                        Form::dkInput('mch_id', '商户号mch_id', $res['mch_id'] ?? '', ['微信支付分配的商户号'])->required(),
                        Form::dkInput('key', '秘钥key', $res['key'] ?? '', ['设置微信支付的Api秘钥'])->required(),
                        Form::dkUpload('pem_cert', 'cert证书', $res['pem_cert'] ?? [])->help(['apiclient_cert.pem 证书'])->required()
                            ->fileType('cert')->data(['path' => "wechat/" . $plaid, 'disk' => 'cert'])->col(Form::col()->span(12)->xs(24)),
                        Form::dkUpload('pem_key', "key证书", $res['pem_key'] ?? [])->help(['apiclient_key.pem 证书'])->required()
                            ->fileType('cert')->data(['path' => "wechat/" . $plaid, 'disk' => 'cert'])->col(Form::col()->span(12)->xs(24))
                    ]
                );
            $rule[] = Form::dkInput('desc', '默认付款描述', $res['desc'] ?? '返利红包', ['设置企业付款到零钱时候的默认描述，如果不填写，则默认为：返利红包'])->required();
            $rule[] = Form::dkInputNumber('pay_time', '自动打款数', $res['pay_time'] ?? 3, ['设置每日自动打款到零钱次数，超出次数后将不会自动打款'])->min(1)->required();
            $rule[] = Form::dkInputNumber('min_money', '最低提现金额', $res['min_money'] ?? 0.3, "设置当前公众号下的个人中心提现最低提现金额")->min(0.3)->step(0.1)->required();
            $rule[] = Form::dkInput('min_money_notice', '提现错误提示', $res['min_money_notice'] ?? "您不满足提现条件，当前最低提现金额为xx", "如果不满足最低提现金额错误提示")->required();
            $form->setTitle("支付配置")->setRule($rule);
            return $this->success("", Form::getFormData($form));
        }
    }

    /**
     * 支付配置表单
     * @return \think\response\Json
     */
    public function preference()
    {
        $plaid = $this->request->param('plaid', 0);
        if ($this->request->isPost()) {
            $formData = $this->request->param('formData', []);
            $res = $this->model->setValueByKey($formData, "preference", $plaid);
            return $this->success("修改成功", $res);
        } else {
            $validate = Form::validateStr();
            $validate->pattern('^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:~\+#]*[\w\-\@?^=%&~\+#])?$')->message('请填写正确的域名，带有http或者https,结尾不要有/');
            $rule = array();
            //创建表单
            $res = $this->model->getValueByKey("preference", $plaid);
            $form = Form::createForm("/adminapi/platform/wechat/setting/preference?plaid=" . $plaid)->setMethod("post");
            //新增表单
            $rule[] = Form::dkInput('oauth_domain', '授权域名', $res['oauth_domain'] ?? '', "设置网页授权域名，网页授权域名对应上公众号设置-功能设置-网页授权域名")
                ->appendValidate($validate);
            $rule[] = Form::dkInput('share_domain', '分享域名', $res['share_domain'] ?? '', "设置为主域名，作用为分享和生成海报的时候的域名，如果落地域名被误封后，不会影响已经分享过的卡片和海报等")
                ->appendValidate($validate);
            $rule[] = Form::dkInput('die_domain', '落地域名', $res['die_domain'] ?? '', "落地域名，炮灰域名，防封域名，该域名为最终用户访问的域名")
                ->appendValidate($validate);
            $form->setTitle("偏好设置")->setRule($rule);
            return $this->success("", Form::getFormData($form));
        }
    }
}
